package com.sgb.kjwl.model.entity.enterprise;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.swgk.core.util.UiUtil;
import com.sgb.kjwl.view.ui.fragment.enterprise.ProfessionalTeamFragment2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2020/12/30 0030
 * 说明:专业团队
 */
public class ProfessionalModel extends BaseViewModel {

    private final ProfessionalTeamFragment2 mFragment;
    private final HomeRepertory mRepertory;
    private int pageNum;

    public ProfessionalModel(ProfessionalTeamFragment2 fragment, HomeRepertory repertory) {
        mFragment = fragment;
        mRepertory = repertory;
    }


    /**
     * 获取服务城市
     */
    public void getServiceCity() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.CallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                if (data != null && data.size() > 0) {
                    mFragment.getCityList(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {

            }
        });

//        Type type = new TypeToken<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>() {}.getType();
//        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.AREA_JSON, type);
//        if (obj != null){
//            List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> dataList = (List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>) obj;
//            AddressEntity entity = new AddressEntity();
//            entity.setNo("");
//            entity.setName("全国");
//            dataList.add(0, entity);
//            mFragment.getCityList(dataList);
//        }else {
//            EnterpriseAPIManager.getInstance().getGoodsAPI().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
//                @Override
//                public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
//                    if (data.getData() == null) return;
//                    List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> list = new ArrayList<>();
//                    list.addAll(data.getData());
//                    AddressEntity entity = new AddressEntity();
//                    entity.setNo("");
//                    entity.setName("全国");
//                    list.add(0, entity);
//                    mFragment.getCityList(list);
//                }
//            });
//        }
    }





    /**
     * 商标列表查询
     */
    public void getTeamQueryList(Map<String, Object> map, boolean isRefresh) {
        if (isRefresh){
            mFragment.loading();
        }
        pageNum = isRefresh ? 1 : pageNum + 1;
        //pageSize
        map.put("limit", 20);
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTeamQueryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TeamChangeEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamChangeEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamChangeEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showList(response.body().getData().getList(), isRefresh);
                    mFragment.success();
                } else {
                    MToast.showToast(UiUtil.getContext(), response.body().getMsg());
                    mFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamChangeEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                mFragment.error();
            }
        });
    }
} 